import fs from 'fs';
import prompts from 'prompts';
import { runPlayer } from './audioPlayer.js';
import { JsonFileManager } from './utils.js';

function isValidWindowsFolderPath(inputPath) {
  return fs.existsSync(inputPath);
}



const configJsonManager = new JsonFileManager('./config.json');
const configJson = configJsonManager.read();

const dataJsonManager = new JsonFileManager('./data.json');
const dataJson = dataJsonManager.read();

const questions = [
  {
    type: 'text',
    name: 'path',
    message: '请输入文件夹地址(右键粘贴)',
    initial: configJson.audioFolder,
    validate: path => isValidWindowsFolderPath(path) ? true : `请输入正确的文件夹地址`
  },
  {
    type: 'confirm',
    name: 'isRePlay',
    message: '是否继续上一次播放?',
    initial: true
  },
];

prompts(questions).then(data => {
  const { path, isRePlay } = data;
  let rePlayList;

  rePlayList = isRePlay ? dataJson['playList'] : [];

  console.log(rePlayList);

  dataJsonManager.update(old => ({ ...old, playList: rePlayList }));
  configJsonManager.update(old => ({ ...old, audioFolder: path }));

  const { isLoopEnabled, replayUrl, uid, dodge, volume } = configJson;

  runPlayer({ audioFolder: path, playedHistory: rePlayList, isLoopEnabled, replayUrl, uid, dodge, volume });
});
